package com.example.baiguangsystem.constroller;

import com.example.baiguangsystem.entity.Author;
import com.example.baiguangsystem.service.AuthorService;
import com.example.baiguangsystem.vo.ResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 作者控制器类
 *
 * @Author: 27641
 * @ProjectName: BaiGuangSystem
 * @Date: 2022/02/04/14:48
 * @noinspection SpellCheckingInspection
 */
@Controller
@RequestMapping("/author")
public class AuthorController {

    /**
     * 注入依赖
     */
    @Autowired
    private AuthorService authorService;

    /**
     * 根据作者Id查找作者信息
     *
     * @param authorId 需要查看的作者Id
     * @return 查询结果
     */
    @ResponseBody
    @GetMapping("/findAuthorById/{authorId}")
    public ResultType findAuthorById(@PathVariable Integer authorId) {
        Author author = this.authorService.selectAuthorById(authorId);
        if (author == null) {
            return ResultType.fail("查找作者失败!");
        }
        return ResultType.success("查找作者成功！", author);
    }

    /**
     * 根据作者名字查看作者信息
     *
     * @param authorName 需要查找的作者名字
     * @return 查找结果
     */
    @GetMapping("/findAuthorByName/{authorName}")
    @ResponseBody
    public ResultType findAuthorByName(@PathVariable String authorName) {
        Author author = this.authorService.selectAuthorByName(authorName);
        if (author == null) {
            return ResultType.fail("查找作者信息失败！");
        }
        return ResultType.success("查找作者成功！", author);
    }

    /**
     * 新增作者
     *
     * @return 操作结果
     */
    @PostMapping("/newAddAuthor")
    @ResponseBody
    public ResultType newAddAuthor(@RequestBody Author author) {
        int addAuthorNumber = this.authorService.newAddAuthor(author);
        if (addAuthorNumber != 1) {
            return ResultType.fail("新增失败！");
        }
        return ResultType.success("新增成功！", author);
    }

    /**
     * 根据作者Id删除作者
     *
     * @param authorId 需要删除
     * @return 操作结果
     */
    @GetMapping("/deleteAuthorById/{authorId}")
    @ResponseBody
    public ResultType deleteAuthorById(@PathVariable Integer authorId) {
        Author author = this.authorService.deleteAuthorById(authorId);
        if (author != null) {
            return ResultType.success("删除作者成功！", author);
        }
        return ResultType.fail("删除作者失败！");
    }

    /**
     * 修改作者信息
     *
     * @param author 需要修改的作者信息
     * @return 修改结果
     */
    @PostMapping("/updateAuthorById")
    @ResponseBody
    public ResultType updateAuthorById(@RequestBody Author author) {
        int authorNumber = this.authorService.updateAuthorById(author);
        if (authorNumber != 1) {
            return ResultType.fail("修改作者信息失败！");
        }
        return ResultType.success("修改作者信息成功！", author);
    }
}
